package com.jdk8;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * 以前日期型，1.混乱，分布大多个包， 2.不是线程安全的
 *  java.util.Date
 *  java.sql.Date/Time/Timestamp
 *  java.text.SimpleDateFormat
 *
 *   一 . java.time.LocalDate/LocalTime/LocalDateTime 对日期基本操作
 *   二 . 时间差 Duration
 *   三.  时间戳 Instant
 *
 *
 */

public class NewTime {
    public static void main(String[] args) {
        //现在日期
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        //现在时间
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);
        //现在日期和时间
        LocalDateTime localDateTime = LocalDateTime.now();
        String timeStr = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(timeStr);

        //指定日期初始化
        LocalDate localDateGuoqing =  LocalDate.of(2020,10,1);
        System.out.println(localDateGuoqing);

        //字符串转日期
        String strOld = "2020-10-22";
        LocalDate localDate1022= LocalDate.parse(strOld);
        System.out.println(localDate1022);

        String strOld2 = "2020-10-22T08:09:10";
        LocalDateTime localDateTime1022 = LocalDateTime.parse(strOld2);
        System.out.println(localDateTime1022);

        String strOld3 = "2020-10-22 08:09:10";
        LocalDateTime localDateTimeStrOld3 = LocalDateTime.parse(strOld3,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(localDateTime1022);

        //获取日期的年，月，日 ，时，分，秒
        int year = localDateTimeStrOld3.getYear();
        int month = localDateTimeStrOld3.getMonthValue();
        int date  = localDateTimeStrOld3.getDayOfMonth();
        int hour =  localDateTimeStrOld3.getHour();
        int minute = localDateTimeStrOld3.getMinute();
        int second = localDateTimeStrOld3.getSecond();
        System.out.println(year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second);

        //获取时间戳（1970-1-1到现在的毫秒数) java.time.Instant 时间戳操作类
        long haomiao = localDateTimeStrOld3.toInstant(ZoneOffset.of("+8")).getEpochSecond();
        System.out.println(haomiao);

        //时间戳转日期
        Instant instant = Instant.ofEpochSecond(haomiao); //时间戳转对象
        LocalDateTime localDateTimeInstant = LocalDateTime.ofInstant(instant, ZoneId.of("+8"));
        System.out.println(localDateTimeInstant);

        //设置时间部分
        LocalDateTime localDateTimeNow = LocalDateTime.now();
        localDateTimeNow =localDateTimeNow.withDayOfMonth(1);
        localDateTimeNow =localDateTimeNow.withHour(0);
        localDateTimeNow =localDateTimeNow.withMinute(0);
        localDateTimeNow =localDateTimeNow.withSecond(0);
        localDateTimeNow =localDateTimeNow.withNano(0);
        System.out.println(localDateTimeNow);

        //日期加减
        localDateTimeNow = localDateTimeNow.plusDays(2); //日期+2
        System.out.println(localDateTimeNow);

        localDateTimeNow = localDateTimeNow.minusDays(2);//日期-2
        System.out.println(localDateTimeNow);


        //日期差,今年还有多少天过年(阳历)
        LocalDateTime begin = LocalDateTime.now();
        LocalDateTime end   = begin.withMonth(12).withDayOfMonth(31); //2020-12-31
        Duration duration =  Duration.between(begin,end);
        long dates = duration.toDays();
        System.out.println(dates);





    }
}
